﻿using System.Diagnostics.Contracts;
using System.IO;
using System.Xml.Linq;
using T4UtilityBelt.Serializers;
using T4UtilityBelt.UxDesigns;
using T4UtilityBelt.Writers;

namespace T4UtilityBelt.Templates
{
    public abstract class UxTemplate : Template
    {
        #region Properties

        private UxDesign _uxDesign;
        /// <summary>
        /// 
        /// </summary>
        protected UxDesign UxDesign
        {
            get
            {
                return _uxDesign;
            }
        }

        public ViewCollection Views
        {
            get { return UxDesign.Views; }
        }


        #endregion


        #region Methods


        protected virtual UxDesign LoadUxDesign()
        {
            return new UxDesign();
        }


        public override void Initialize()
        {
            base.Initialize();

            InitializeUxDesign();
            if (_uxDesign != null)
            {
                _uxDesign.FixModel(Model);
            }
        }

        protected virtual void InitializeUxDesign()
        {
            _uxDesign = LoadUxDesign();
        }

        protected void LoadUxDesign(string fileName)
        {
            XSerializer serializer = new XSerializer();

            UxDesign model = serializer.Deserialize<UxDesign>(XDocument.Load(Path.Combine(TemplatePath, fileName)));

            if (_uxDesign == null)
            {
                _uxDesign = model;
            }
            else
            {
                if (model.Views != null)
                {
                    foreach (View view in model.Views)
                    {
                        _uxDesign.Views.Add(view);
                    }
                }
            }
        }


        protected void LoadViews(string folder)
        {
            string[] files = Directory.GetFiles(Path.Combine(TemplatePath, folder), "*.tlvx");

            XSerializer serializer = new XSerializer();

            foreach (string file in files)
            {
                View view = serializer.Deserialize<View>(XDocument.Load(file));

                if (_uxDesign == null)
                {
                    _uxDesign = new UxDesign();
                }

                _uxDesign.Views.Add(view);
            }
        }

        protected void LoadView(string fileName)
        {
            XSerializer serializer = new XSerializer();

            View view = serializer.Deserialize<View>(XDocument.Load(Path.Combine(TemplatePath, fileName)));

            if (_uxDesign == null)
            {
                _uxDesign = new UxDesign();
            }

            _uxDesign.Views.Add(view);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public void RenderUiXml(Control control)
        {
            Contract.Assert(control != null);


            CodeWriter writer = new CodeWriter(Platform);
            control.RenderUiXml(writer);
            Write(writer.ToString());
        }

        #endregion

    }
}
